using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class UIBuild : MonoBehaviour
{
    public List<BuildData> buildDatas = new List<BuildData>();

    public RectTransform rect;

    public List<Sprite> numberSprite = new List<Sprite>();

    void Awake()
    {
        rect = GetComponent<RectTransform>();
    }
    // Start is called before the first frame update
    void Start()
    {
        foreach (var buildData in buildDatas)
        {
            buildData.BuildRoot.gameObject.SetActive(false);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void Build(BuildType buildType)
    {
        var buildData = buildDatas.Find(x => x.buildType == buildType);
        buildData.IsBuilding = true;
        for (int i = 0; i < buildData.needResourceDatas.Count; i++)
        {
            var needResourceData = buildData.needResourceDatas[i];
            int CurrentHaveResourceCount = GameEntry.Instance.ResourceCounts[needResourceData.resourceState]; //当前玩家拥有的这个资源的数量
            int SpendResourceCount = 0; //花费的资源数量 
            int NeedResourceCount = needResourceData.NeedResourceCount - needResourceData.CurrentResourceCount; // 建造需要的资源数量 
            if (CurrentHaveResourceCount >= NeedResourceCount) //资源够用
            {
                SpendResourceCount = NeedResourceCount;
            }
            else //资源不够用，使用当前拥有的资源数量
            {
                SpendResourceCount = CurrentHaveResourceCount;
            }

            GameEntry.Instance.AddResrouce(needResourceData.resourceState, -SpendResourceCount);

            //令当前资源已经拥有的数量加上花费资源的数量
            DOTween.To(() => needResourceData.CurrentResourceCount, x => needResourceData.CurrentResourceCount = x,
            needResourceData.CurrentResourceCount + SpendResourceCount, 0.5f).OnUpdate(() =>
            {
                RefreshNumberSprite(needResourceData.Build_Resource_Number1, needResourceData.Build_Resource_Number2,
                 needResourceData.NeedResourceCount - needResourceData.CurrentResourceCount);
                float fillAmount = buildData.GetLvseFillAmound();
                buildData.Build_Lvse.fillAmount = fillAmount; //更新进度条的填充量

                buildData.BuildRoot.transform.DOKill();
                buildData.BuildRoot.transform.localScale = new Vector3(1.1f, 1.1f, 1.1f);
                buildData.BuildRoot.transform.DOScale(Vector3.one,0.15f);
            }).OnComplete(() =>
            {
                if (buildData.needResourceDatas.TrueForAll(x => x.CurrentResourceCount >= x.NeedResourceCount)) //如果所有资源都满足建造条件
                {
                    buildData.IsBuilding = false;
                    buildData.IsBuild = true;
                    buildData.BuildRoot.gameObject.SetActive(false);
                    // GameEntry.Instance.OnBuildComplete(buildData.buildType); //通知游戏逻辑建造完成
                    BuildCompleteEvent buildCompleteEvent = new BuildCompleteEvent();
                    buildCompleteEvent.buildCompleteType = buildType;
                    EventManager.Instance.Fire(this,buildCompleteEvent);
                }
            });
        }
        

    }
    
    public void RefreshNumberSprite(Image Number1, Image Number2, int Count)
    {
        int a = Count / 10;
        int b = Count % 10;
        if (a == 0)
        {
            Number1.gameObject.SetActive(false);
        }
        else
        {
            Number1.gameObject.SetActive(true);
            Number1.sprite = numberSprite[a];
            Number1.rectTransform.sizeDelta = new Vector2(numberSprite[a].texture.width, numberSprite[b].texture.height);
        }
        Number2.gameObject.SetActive(true);
        Number2.sprite = numberSprite[b];
        Number2.rectTransform.sizeDelta = new Vector2(numberSprite[b].texture.width, numberSprite[b].texture.height);
    }
}
